import { getElHeight } from "./utils.js";

const useSplitPage = () => {
  const split = (options = {}) => {
    const { rootSelector, targetHeight } = options;

    /** @type {HTMLElement} */
    const rootEl = document.querySelector(rootSelector);

    if (!rootEl) {
      console.error(`rootSelector ${rootSelector} 对应的元素不存在`);
      return;
    }

    console.log(`待分隔的元素一共有${rootEl.children.length}条`);
    console.log(`目标高度为${targetHeight}px`);

    // 先尝试这一整页能否 塞进去
    const firstElHeight = getElHeight(rootEl.children[0]);
    if (targetHeight >= firstElHeight) {
      console.log("这一整页可以塞进去");
      return;
    }

    const toSplitEl = rootEl.children[0]
    console.log(`第一条元素的高度为${firstElHeight}px`,toSplitEl.children);
  };

  return {
    split,
  };
};

export { useSplitPage };
